package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.table.DepartmentTable;

import java.util.List;

/**
 * @author 000432
 * @date 2017/4/19 0019
 *
 * 建包查询网点adapter
 */

public class CreateDepteSelectAdapter extends BaseAdapter {

  private Context mContext;
  private List<DepartmentTable> mDataList;
  private int selectPosition = -1;

  public CreateDepteSelectAdapter(Context context, List<DepartmentTable> mDataList) {
    this.mContext = context;
    this.mDataList = mDataList;
  }

  public List<DepartmentTable> getDataList() {
    return mDataList;
  }

  public void setDataList(List<DepartmentTable> dataList) {
    mDataList = dataList;
  }

  public void setSelectPosition(int position) {
    this.selectPosition = position;
  }

  @Override public View getView(int i, View convertView, ViewGroup viewGroup) {
    ViewHolder viewHolder;
    if (convertView == null) {
      viewHolder = new ViewHolder();
      convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_dept_select_item, null);
      viewHolder.deptCode = (TextView) convertView.findViewById(R.id.tv_left);
      viewHolder.deptName = (TextView) convertView.findViewById(R.id.tv_right);
      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    DepartmentTable item = mDataList.get(i);
    viewHolder.deptCode.setText(item.getDeptCode());
    viewHolder.deptName.setText(item.getDeptName());

    if (selectPosition == i) {
      convertView.setBackgroundResource(R.color.red);
    } else {
      convertView.setBackgroundColor(Color.TRANSPARENT);
    }
    return convertView;
  }

  public static class ViewHolder {
    /** 网点编号 */
    TextView deptCode;
    /** 网点名称 */
    TextView deptName;
  }

  @Override public int getCount() {
    return mDataList.size();
  }

  @Override public Object getItem(int i) {
    return mDataList.get(i);
  }

  @Override public long getItemId(int i) {
    return i;
  }
}
